package com.gitee.wsl.concurrent.scheduler

import com.gitee.wsl.concurrent.api.cancel.Cancellable
import com.gitee.wsl.concurrent.executor.Executor
import kotlinx.coroutines.Runnable
import kotlin.time.Duration

/**
 * Base interface for [Scheduler] executors. All tasks are executed synchronously one by one, never concurrently.
 * [SchedulerExecutor]s must be disposed when they are no longer needed.
 */
interface SchedulerExecutor : Executor, Cancellable {

    /**
     * Submits a new task, repeating if [period] is specified.
     *
     * @param delay a delay before first execution, default is [Duration.Companion.ZERO] (as soon as possible).
     * @param period a period between executions, default is [Duration.Companion.INFINITE] (non-repeating).
     * @param task the task to be executed
     */
    fun submit(
        delay: Duration = Duration.ZERO,
        period: Duration = Duration.INFINITE,
        task: Runnable,
    )

    /**
     * Cancels all tasks. All running tasks will be interrupted, all pending tasks will not be executed.
     */
    //fun cancel()

    override fun exec(runnable: Runnable) = submit( task = runnable )

}